﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _404park.Core.Content;
using _404park.Core.DataInterface;
using _404park.Data.Content;
using _404park.Utilities.Extensions;
using _404park.Utilities.Web;
using System.Text.RegularExpressions;

namespace _404park.ApplicationServices.Content
{
    public class KeywordService : IKeywordService
    {
        private readonly IKeywordRepository _keywordRepository = new KeywordRepository();
        private readonly static IDictionary<string, string> _meaninglessWordMap = new Dictionary<string, string>() { 
            {"www1", string.Empty},
            {"-", " "},
            {"_", " "},
            {".", " "},
        };

        private readonly static IDictionary<string, string> _wordMap = new Dictionary<string, string>() { 
            {"ad56 nettravel", "du lịch"},
            {"nonexistent getwebcake", "công cụ web"},
            {"nonexistent yontoo", "công cụ web"},
            {"mail vpb", "tuyển dụng ngân hàng"}
        };

        public IList<Keyword> GetTopKeywords(int limit)
        { 
            var keywords = _keywordRepository.GetAllKeywords();
            if (keywords == null) return null;
            var selectedKeywords = keywords.PlayDice((x) => { return x.Rating; }, new Random(), limit);

            return selectedKeywords;
        }

        public IList<Tag> GetTopTags(int tagLimit)
        {
            var keywords = _keywordRepository.GetAllKeywords();
            var tags = keywords.Select(x => x.Tags[0]).Distinct().ToList();
            var selectedTags = tags.PlayDice((x) => { return 1; }, new Random(), tagLimit);
            

            return selectedTags;
        }

        public string BuildKeywords(string host)
        {
            var searchTerm = HostResolver.Instance().GetSearchTerm(host);
            var keywords = RemoveMeaninglessWords(searchTerm);
            keywords = ReMapKeyword(keywords);
            return string.IsNullOrEmpty(keywords) ? host : keywords;
        }

        #region Build Keywords

        private string RemoveMeaninglessWords(string searchTerm)
        {
            var keywords = new StringBuilder(searchTerm);

            foreach (KeyValuePair<string, string> item in _meaninglessWordMap)
            {
                if (keywords.ToString().Contains(item.Key))
                    keywords = keywords.Replace(item.Key, item.Value);
            }

            return keywords.ToString().Trim().RemoveSpaces();
        }

        private string ReMapKeyword(string searchTerm)
        {
            var keywords = new StringBuilder(searchTerm);

            foreach (KeyValuePair<string, string> item in _wordMap)
            {
                if (keywords.ToString().Equals(item.Key, StringComparison.InvariantCultureIgnoreCase))
                    keywords = keywords.Replace(item.Key, item.Value);
            }

            return keywords.ToString().Trim().RemoveSpaces();
        }

        #endregion
    }
}
